package com.eqdd.yiqidian.ui.yiqixue.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewFragmentCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LearnMineInfo;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditIconBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.mine.renwu.TextActivity;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.PicUtil;

import java.util.ArrayList;

/**
 * @author吕志豪 .
 * @date 18-2-9  下午1:16.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class LearnMineInfoFragment extends BaseFragment {

    private RecyclerViewFragmentCustom dataBinding;
    private LearnMineInfo learnMineInfo;
    private SlimAdapterEx<SlimEditBean> slimAdapterEx;
    private Bundle bundle;

    @Override
    protected void setView() {

    }

    protected String dealText(String text) {
        return TextUtils.isEmpty(text) ? "" : text;
    }

    @Override
    protected void initData() {
        if (slimAdapterEx == null) {
            learnMineInfo = (LearnMineInfo) getArguments().getSerializable(Config.BEAN);
            ArrayList<SlimEditBean> slimEditBeans = new ArrayList<>();
            slimEditBeans.add(new SlimEditIconBean("头像", learnMineInfo.getHeadimage(), 0));
            slimEditBeans.add(new SlimEditHintBean("姓名", learnMineInfo.getRealname(), 1));
            slimEditBeans.add(new SlimEditHintBean("手机", learnMineInfo.getPhone(), 2));
            slimEditBeans.add(new SlimEditHintBean("助理手机", learnMineInfo.getAssistantPhone(), 3));
            slimEditBeans.add(new SlimEditHintBean("助理姓名", learnMineInfo.getAssistant(), 4));
            slimEditBeans.add(new SlimEditHintBean("邮箱", learnMineInfo.getEmail(), 5));
            slimEditBeans.add(new SlimEditHintBean("QQ", learnMineInfo.getQQ(), 6));
            slimEditBeans.add(new SlimEditHintBean("微信", learnMineInfo.getWechat(), 7));
            slimEditBeans.add(new SlimEditHintBean("常驻地址", learnMineInfo.getAddress(), 8));
            slimEditBeans.add(new SlimEditHintBean("研究领域", learnMineInfo.getResearchField(), 9));
            slimEditBeans.add(new SlimEditHintBean("课程价格(元/天)", learnMineInfo.getCooperativePrice() + "元/天", 10));
            slimEditBeans.add(new SlimEditEnterBean("课程", learnMineInfo.getCourses(), 11, true, "查看"));
            slimEditBeans.add(new SlimEditEnterBean("授课风格", learnMineInfo.getTeachStyle(), 12, true, "查看"));
            slimEditBeans.add(new SlimEditEnterBean("讲师简介", "", 13, true, "查看"));
            slimEditBeans.add(new SlimEditEnterBean("客户案例", "", 14, true, "查看"));
            slimEditBeans.add(new SlimEditEnterBean("曾服务过的企业", "", 15, true, "查看"));
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
            );
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_23, new SlimInjector<SlimEditIconBean>() {
                @Override
                public void onInject(SlimEditIconBean data, IViewInjector injector) {
                    injector.with(R.id.tv_left_content, view -> {
                        TextView textView = (TextView) view;
                        if (dealText(data.getTitle()).indexOf("*") == 0) {
                            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(dealText(data.getTitle()));
                            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);

                            spannableStringBuilder.setSpan(foregroundColorSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                            textView.setText(spannableStringBuilder);
                        } else {
                            textView.setText(dealText(data.getTitle()));
                        }
                    })
                            .with(R.id.iv_right_logo, v -> {
                                if (TextUtils.isEmpty(data.getContent())) {
                                    ImageUtil.setImage(data.getIcon(), (ImageView) v);
                                } else {
                                    ImageUtil.setCircleImage(data.getContent(), (ImageView) v);
                                }
                            })
                            .clicked(R.id.iv_right_logo, v -> PicUtil.previewSingle(getActivity(), PicUtil.localMedia(data.getContent())));
                }
            }).register(R.layout.list_item_28, new SlimInjector<SlimEditHintBean>() {
                @Override
                public void onInject(SlimEditHintBean data, IViewInjector injector) {
                    injector.with(R.id.tv_left_content, view -> {
                        TextView textView = (TextView) view;
                        if (dealText(data.getTitle()).indexOf("*") == 0) {
                            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(dealText(data.getTitle()));
                            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);

                            spannableStringBuilder.setSpan(foregroundColorSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                            textView.setText(spannableStringBuilder);
                        } else {
                            textView.setText(dealText(data.getTitle()));
                        }
                    })
                            .text(R.id.tv_right_hint_content, dealText(data.getContent()));
                }
            }).register(R.layout.list_item_29, new SlimInjector<SlimEditEnterBean>() {
                @Override
                public void onInject(SlimEditEnterBean data, IViewInjector injector) {
                    injector.with(R.id.tv_left_content, view -> {
                        TextView textView = (TextView) view;
                        if (dealText(data.getTitle()).indexOf("*") == 0) {
                            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(dealText(data.getTitle()));
                            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
                            spannableStringBuilder.setSpan(foregroundColorSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                            textView.setText(spannableStringBuilder);
                        } else {
                            textView.setText(dealText(data.getTitle()));
                        }
                    })
                            .text(R.id.tv_right_content, dealText(data.getContent()))
                            .with(R.id.tv_right_content, v -> ((TextView) v).setHint(dealText(data.getHintContent())));
                }
            }).attachTo(dataBinding.recyclerView).updateData(slimEditBeans);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        switch (slimAdapterEx.getDataItem(position).getPosition()) {
                            case 11:
                                bundle = new Bundle();
                                bundle.putString(Config.CONTENT, learnMineInfo.getCourses());
                                bundle.putString(Config.TITLE, "课程");
                                startActivity(TextActivity.class, bundle);
                                break;
                            case 12:
                                bundle = new Bundle();
                                bundle.putString(Config.CONTENT, learnMineInfo.getTeachStyle());
                                bundle.putString(Config.TITLE, "授课风格");
                                startActivity(TextActivity.class, bundle);
                                break;
                            case 13:
                                bundle = new Bundle();
                                bundle.putBoolean(Config.IS_HTML,true);
                                bundle.putString(Config.CONTENT, learnMineInfo.getLecturerBackground().replace("\\",""));
                                bundle.putString(Config.TITLE, "讲师简介");
                                startActivity(TextActivity.class, bundle);
                                break;
                            case 14:
                                bundle = new Bundle();
                                bundle.putString(Config.CONTENT, learnMineInfo.getCustCase().replace("\\",""));
                                bundle.putBoolean(Config.IS_HTML,true);
                                bundle.putString(Config.TITLE, "客户案例");
                                startActivity(TextActivity.class, bundle);
                                break;
                            case 15:
                                bundle = new Bundle();
                                bundle.putString(Config.CONTENT, learnMineInfo.getServiceCom().replace("\\",""));
                                bundle.putString(Config.TITLE, "曾经服务过的企业");
                                bundle.putBoolean(Config.IS_HTML,true);
                                startActivity(TextActivity.class, bundle);
                                break;
                        }
                    });
        }
    }

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_recycler_view, null, false);
    }

    @Override
    public void onClick(View v) {

    }
}
